const express = require('express')

const list = express.Router()

const User = require('../model/user.js')

list.get('/', async(req, res) => {
    let users = await User.find()
    let list = `
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <link rel="stylesheet" href="http://localhost:3000/static/bootstrap.min.css">
    <style>
        body {
            background-color: #999;
        }
        .btn{
            diaplay: block;
            margin: 10px 0;
            
        }
    </style>

</head>

<body>
    <div class="container">
        <h6>
            <a href="/add" class="btn btn-primary">添加用户</a>
        </h6>
        <table class="table table-striped table-bordered">
            <tr>
                <td>用户名</td>
                <td>年龄</td>
                <td>爱好</td>
                <td>邮箱</td>
                <td>操作</td>
            </tr>`

    users.forEach(item => {
        list +=
            `
                <tr>
                <td>${item.name}</td>
                    <td>${item.age}</td>
                    <td>
            `
        item.hobbies.forEach(item => {

            list +=
                `
                    <span>${item}&nbsp;&nbsp;  </span>
                `
        })
        list +=
            `
            </td>        
            <td>${item.email}</td>
            <td>
                <a href="/remove?id=${item._id}" class="btn btn-danger btn-xs">删除</a>
                <a href="/modify?id=${item._id}" class="btn btn-success btn-xs">修改</a>
            </td>
            </tr>`
    })

    list += `
    
    </table>
    </div>
    </body>

    </html>
    `
    res.send(list)
})

module.exports = list